package validator

import (
	"github.com/go-playground/validator/v10"
	"github.com/zeromicro/go-zero/rest/httpx"
	"net/http"
	"sync"
)

type customValidator struct {
	once     sync.Once
	validate *validator.Validate
}

//	func (c *customValidator) Validate(i interface{}) error {
//		c.lazyInit()
//		return c.validate.Struct(i)
//	}

func (c *customValidator) Validate(r *http.Request, data any) error {
	c.lazyInit()
	return c.validate.Struct(data)
}

func (c *customValidator) lazyInit() {
	c.once.Do(func() {
		c.validate = validator.New()
	})
}

func Register() {
	httpx.SetValidator(&customValidator{})
}
